$(document).ready(function () {
    initMenuTree(); //加载菜单树,根据登录用户所属机构查询
});

/**
 * 加载菜单树
 */
function initMenuTree() {
    $.ajax({
        type: "GET",
        dataType: "json",
        url: "user/getMenuByJgbm",
        success: function (jsonData) {
            if (jsonData.code == 200) {   //查询成功
                $.fn.zTree.init($("#tree_Menu"), setting, jsonData.menuTreeList);

                fuzzySearch('tree_Menu', '#cdss', null, true); //初始化模糊搜索方法

                var treeObj = $.fn.zTree.getZTreeObj("tree_Menu");
                var nodes = treeObj.getNodes();

                if (nodes.length > 0) {
                    //默认展开三级节点
                    if (nodes[0].children!=null && nodes[0].children.length > 0) {
                        nodes = nodes[0].children;
                        if (nodes[0].children!=null && nodes[0].children.length > 0)
                            nodes = nodes[0].children;
                    }
                    treeObj.expandNode(nodes[0], true, false, true);
                    //默认选中根节点
                    treeObj.selectNode(treeObj.getNodes()[0]);

                    var treeObj = $.fn.zTree.getZTreeObj("tree_Menu");
                    var nodes = treeObj.getSelectedNodes();
                    var cddm = nodes[0].id;  //获取菜单代码
                    if(cddm!=null){
                        queryQxyhList();
                    }

                }
            } else {
                LayerWn.showFailAlert(jsonData.msg);
            }
        }
    });
}

var setting = {
    view: {
        fontCss: {"font-family": "Microsoft YaHei"},
        selectedMulti: false,
        showIcon: false
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        onClick: zTreeOnClick
    }
};

/**
 * 点击菜单树
 */
function zTreeOnClick(event, treeId, treeNode) {
    queryQxyhList();
};

/**
 * /查询已开通菜单权限的用户
 */
function queryQxyhList() {
    queryHandler();
}


/**
 * 初始化列表
 */
function queryHandler() {

    var treeObj = $.fn.zTree.getZTreeObj("tree_Menu");
    var nodes = treeObj.getSelectedNodes();
    var cddm = '';
    if(nodes.length >0){
         cddm = nodes[0].id;  //获取菜单代码
    }else{
        LayerWn.showAlert('未选中菜单！');
        return;
    }
    var ryxm = $("#ryxm").val();
    var rygh = $("#rygh").val();
    var queryParms = {ryxm: ryxm, rygh: rygh, cddm: cddm};

    $('#tb').bootstrapTreeTable({
        id: 'ID',
        // parentColumn: 'ParentId',
        code : 'ID',// 用于设置父子关系
        parentCode : 'ParentId',// 用于设置父子关系
        type: "POST", //请求数据的ajax类型
        url: 'user/queryQxyhList',   //请求数据的ajax的url
        ajaxParams: queryParms, //请求数据的ajax的data属性
        expandColumn: 0,//在哪一列上面显示展开按钮
        striped: false,   //是否各行渐变色
        bordered: false,  //是否显示边框
        classes:'table-no-bordered',
        expandAll: false,  //是否全部展开
        toolbar: null, //顶部工具条
        rootCodeValue: null, //设置根节点code值----可指定根节点，默认为null,"",0,"0"
        height: 0,
        columns: [
            {
                title: '用户姓名',
                field: 'name'
            },
            {
                title: '用户数',
                field: 'zc'
            },
            {
                title: '人员工号',
                field: 'rygh'
            },
            {
                title: '登录名',
                field: 'dldm'
            }
        ]
    });
}

